package com.pcjavanet.collect.mes.dao.impl.jdbc.postgresql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowCallbackHandler;

import com.pcjavanet.collect.mes.dao.AbstractDao;
import com.pcjavanet.collect.mes.dao.OriginalCentralDbDataBySPDao;
import com.pcjavanet.collect.mes.to.ContinuousOriginalData;
import com.pcjavanet.domain.Attribute;
import com.pcjavanet.domain.SPCObject;

public class OriginalCentralDbDataBySPDaoImpl extends AbstractDao implements OriginalCentralDbDataBySPDao{

	protected static Logger logger = Logger.getLogger(OriginalCentralDbDataBySPDaoImpl.class);
	
	
	/**
	 * get continuous original data from SP .
	 * @param monitor -Monitor
	 * @return List<ContinuousOriginalData> -continuousOriginalDataList
	 * @throws DataAccessException -exception
	 */
	public List<ContinuousOriginalData> getContinuousOriginalData(SPCObject monitor) throws DataAccessException {
		final List<ContinuousOriginalData> continuousOriginalDataList = new ArrayList<ContinuousOriginalData>();
//		String attribute=monitor.getAttributeName();
		List<Attribute> attributes=monitor.getAttributes();
		StringBuffer bufAtt = new StringBuffer();
		for( int i=0; i<attributes.size();i++) {
			bufAtt.append("'").append(attributes.get(i).getAttributeName()).append("'");
			if ( i !=( attributes.size() -1 ) ) {
				bufAtt.append(",");
			}  
		}
		String  attribute = bufAtt.toString() ;
//		attribute=attribute.replaceAll(",", "','");
//		attribute="'"+attribute+"'";
		if(!attribute.equals("''")){
			long lastGroupTime = monitor.getLastUpdateTime();
			StringBuffer buf = new StringBuffer();
			buf.append(" select board_id, attribute_name,  height, \"a_DateTime\",part_number ");
			buf.append(" FROM spi_data  where ");
			buf.append(" part_number='"+monitor.getPartName()+"' ");
			buf.append(" AND file_type ='"+monitor.getShortName()+"' ");
			buf.append(" AND attribute_name in ( "+attribute+" )" );
			buf.append(" AND anet_userid='"+monitor.getNetUserId()+"' ");
			buf.append(" AND \"a_DateTime\">"+lastGroupTime);
			buf.append(" ORDER BY \"a_DateTime\" limit 15000 ");
			long startTime = System.currentTimeMillis();
			try {	
				jdbcTemplate.query(
					buf.toString(),	
					new RowCallbackHandler() {
						public void processRow(ResultSet rs) throws SQLException {
							ContinuousOriginalData continuousOriginalData = new ContinuousOriginalData();
							continuousOriginalData.setSerialNumber(rs.getString(1));
							continuousOriginalData.setParam1(rs.getString(2));
							continuousOriginalData.setParam2(rs.getString(3));
							continuousOriginalData.setADateTime(rs.getLong(4));
							continuousOriginalData.setPartNumber(rs.getString(5));
							continuousOriginalDataList.add(continuousOriginalData);
						}
					}
				);
//				long endTime = System.currentTimeMillis();
//				logger.info( Thread.currentThread().getName()+";[monitor_key:"+monitor.getMonitorId() +"];[startTime:"+startTime+" - endTime:"+endTime+"="+(endTime-startTime)+"];[size:"+continuousOriginalDataList.size()+"]");
//				if(  (endTime - startTime ) > 7000 ) {
//					logger.info( buf.toString() );
//				}
			}catch (Exception e) {
//				long endTime = System.currentTimeMillis();
//				logger.error( Thread.currentThread().getName()+";[monitor_key:"+monitor.getMonitorId() +"];[startTime:"+startTime+" - endTime:"+endTime+":"+(endTime-startTime)+"]");
				logger.error(  e );
			}
		}
		return continuousOriginalDataList;
	}
	
	
}
